package com.github.permissiondog.cancanpandroid.ui.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.github.permissiondog.cancanpandroid.BR;
import com.github.permissiondog.cancanpandroid.R;
import com.github.permissiondog.cancanpandroid.databinding.ActivityLoginBinding;
import com.github.permissiondog.cancanpandroid.model.User;
import com.github.permissiondog.cancanpandroid.repository.UserRepository;
import com.github.permissiondog.cancanpandroid.repository.impl.UserRepositoryImpl;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        var user = new User();
        binding.setVariable(BR.data, user);
        binding.btnLogin.setOnClickListener(l -> {
            Log.i("login", binding.etUsername.getText().toString());
            Log.i("login", binding.etPassword.getText().toString());
            UserRepositoryImpl.getInstance().login(binding.etUsername.getText().toString(), binding.etPassword.getText().toString(), ok -> {
                if (ok) {
                    var intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_LONG).show();
                }
            });

        });
    }
}